//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using NopSolutions.NopCommerce.BusinessLogic.Caching;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Data;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.Localization;
using NopSolutions.NopCommerce.BusinessLogic.Messages;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.BusinessLogic.Alesko
{
    /// <summary>
    /// HtmlContent service
    /// </summary>
    public partial class HtmlContentService : IHtmlContentService
    {
        #region Constants
        private const string HtmlContent_BY_ID_KEY = "Alesko.HtmlContent.id-{0}";
        private const string HtmlContent_PATTERN_KEY = "Alesko.HtmlContent.";
        #endregion

        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly NopObjectContext _context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public HtmlContentService(NopObjectContext context)
        {
            this._context = context;
            this._cacheManager = new NopRequestCache();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets a HtmlContent
        /// </summary>
        /// <param name="ContentId">The Message identifier</param>
        /// <returns>HtmlContent</returns>
        public HtmlContent GetHtmlContentById(int ContentId)
        {
          if (ContentId == 0)
                return null;

            string key = string.Format(HtmlContent_BY_ID_KEY, ContentId);
            object obj2 = _cacheManager.Get(key);
            if (this.CacheEnabled && (obj2 != null))
            {
                return (HtmlContent)obj2;
            }

            
            var query = from n in _context.HtmlContent
                        where n.ContentId == ContentId
                        select n;
            var HtmlContent = query.SingleOrDefault();

            if (this.CacheEnabled)
            {
                _cacheManager.Add(key, HtmlContent);
            }
            return HtmlContent;
        }

        /// <summary>
        /// Deletes a HtmlContent
        /// </summary>
        /// <param name="ContentId">The HtmlContent identifier</param>
        public void DeleteHtmlContent(int ContentId)
        {
            var HtmlContent = GetHtmlContentById(ContentId);
            if (HtmlContent == null)
                return;
            
            if (!_context.IsAttached(HtmlContent))
                _context.HtmlContent.Attach(HtmlContent);
            _context.DeleteObject(HtmlContent);
            _context.SaveChanges();

            if (this.CacheEnabled)
            {
              _cacheManager.RemoveByPattern(HtmlContent_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Gets all HtmlContent
        /// </summary>
        /// <returns>HtmlContent item collection</returns>
        public List<HtmlContent> GetAllHtmlContent()
        {
          return GetAllHtmlContent(0, Int32.MaxValue);
        }

        /// <summary>
        /// Gets HtmlContent item collection
        /// </summary>
        /// <param name="pageSize">Page size</param>
        /// <param name="pageIndex">Page index</param>
        /// <returns>HtmlContent item collection</returns>
        public PagedList<HtmlContent> GetAllHtmlContent(int pageIndex, int pageSize)
        {
            if(pageSize <= 0)
            {
                pageSize = 10;
            }
            if(pageSize == Int32.MaxValue)
            {
                pageSize = Int32.MaxValue - 1;
            }
            if(pageIndex < 0)
            {
                pageIndex = 0;
            }
            if(pageIndex == Int32.MaxValue)
            {
                pageIndex = Int32.MaxValue - 1;
            }
            
            var query = from n in _context.HtmlContent
                        orderby n.ContentId ascending
                        select n;
            var HtmlContent = new PagedList<HtmlContent>(query, pageIndex, pageSize);
            return HtmlContent;
        }

        /// <summary>
        /// Inserts a HtmlContent item
        /// </summary>
        /// <param name="HtmlContent">HtmlContent item</param>
        public void InsertHtmlContent(HtmlContent HtmlContent)
        {
            if (HtmlContent == null)
                throw new ArgumentNullException("HtmlContent");

            HtmlContent.ContentName = CommonHelper.EnsureNotNull(HtmlContent.ContentName);
            HtmlContent.ContentName = CommonHelper.EnsureMaximumLength(HtmlContent.ContentName, 50);
            HtmlContent.ContentHtml = CommonHelper.EnsureNotNull(HtmlContent.ContentHtml);
            
            _context.HtmlContent.AddObject(HtmlContent);
            _context.SaveChanges();

            if (this.CacheEnabled)
            {
              _cacheManager.RemoveByPattern(HtmlContent_PATTERN_KEY);
            }
        }

        /// <summary>
        /// Updates the HtmlContent item
        /// </summary>
        /// <param name="HtmlContent">HtmlContent item</param>
        public void UpdateHtmlContent(HtmlContent HtmlContent)
        {
            if (HtmlContent == null)
                throw new ArgumentNullException("HtmlContent");

            HtmlContent.ContentName = CommonHelper.EnsureNotNull(HtmlContent.ContentName);
            HtmlContent.ContentName = CommonHelper.EnsureMaximumLength(HtmlContent.ContentName, 50);
            HtmlContent.ContentHtml = CommonHelper.EnsureNotNull(HtmlContent.ContentHtml);
                      
            if (!_context.IsAttached(HtmlContent))
                _context.HtmlContent.Attach(HtmlContent);

            _context.SaveChanges();

            if (this.CacheEnabled)
            {
              _cacheManager.RemoveByPattern(HtmlContent_PATTERN_KEY);
            }
        }
        #endregion

        #region Properties

        /// <summary>
        /// Gets a value indicating whether cache is enabled
        /// </summary>
        public bool CacheEnabled
        {
            get
            {
                //return IoC.Resolve<ISettingManager>().GetSettingValueBoolean("Cache.HtmlContentManager.CacheEnabled");
              return false;
            }
        }
        
        /// <summary>
        /// Gets or sets a value indicating whether HtmlContent are enabled
        /// </summary>
        public bool HtmlContentEnabled
        {
            get
            {
                //bool HtmlContentEnabled = IoC.Resolve<ISettingManager>().GetSettingValueBoolean("HtmlContent.HtmlContentEnabled");
                //return HtmlContentEnabled;
              return true;
            }
            /*set
            {
                IoC.Resolve<ISettingManager>().SetParam("HtmlContent.HtmlContentEnabled", value.ToString());
            }*/
        }
        #endregion
    }
}
